<template>
  <j-modal
    title="门户权限授权"
    :visible.sync="visible"
    :width="900"
    :maskClosable="false"
    cancelText="关闭"
    :confirmLoading="confirmLoading"
    @ok="handleOk">
    <eoa-portal-site-ls-role ref="roleRef" @select="onSelectRole"></eoa-portal-site-ls-role>
  </j-modal>
</template>

<script>
  import EoaPortalSiteLsRole from './EoaPortalSiteLsRole'
  import { postAction,getAction } from '@/api/manage'

  export default {
    name: 'EoaPortalSiteAuthSetter',
    components:{
      EoaPortalSiteLsRole,
    },
    data(){
      return {
        visible: false,
        portalSiteId:'',
        portalType:'',
        confirmLoading: false,
        activeKey:'a',
        rightTreeRef:{
          a:'fieldRef',
          b:'buttonRef',
          c:'dataref'
        },
        activeRole:'',
        url:{
          addPortalAuth : '/eoa/cms/eoaPortalSite/addAuth',
          getRoleByPortal : '/eoa/cms/eoaPortalSite/getRoleByPortal'
        },
        dbRoleIds:"" //数据库的角色id
      }
    },
    methods:{
      show(portalSiteId,portalType){
        if(this.portalSiteId && this.portalSiteId!=portalSiteId){
          this.reset();
        }
        this.portalSiteId = portalSiteId
        this.portalType = portalType
        this.visible = true
        this.activeKey = 'a'
        this.checkedKeys = []
        getAction(this.url.getRoleByPortal,{portalId:portalSiteId,portalType:portalType}).then((res)=>{
          if(res.success){
            let arr = []
            for(let k of res.result){
              arr.push(k.roleId)
            }
            //update-begin---author:wangshuai ---date:20220120  for：[JTC-517]门户设置角色授权复选框取消不掉------------
            //初始化角色权限
            this.dbRoleIds = [...arr]
            //update-end---author:wangshuai ---date:20220120  for：[JTC-517]门户设置角色授权复选框取消不掉------------
            this.checkedKeys = [...arr]
            this.$refs.roleRef.selectedRowKeys = this.checkedKeys;
          }
        })

      },
      reset(){
        this.$nextTick(()=>{
          this.$refs.roleRef.clearRow()
        })
      },
      onSelectRole(roleId){
        //选中角色事件'
        this.activeRole = roleId
      },
      onSelectDepart(departid){
        console.log('选中的部门ID',departid)
      },
      authTabChange(value){
        //切换 右侧tab 如果当前选中角色信息需要加载tab内选中信息
        let activeRef = this.rightTreeRef[value]
        this.$nextTick(()=>{
          if(this.activeRole){
            this.$refs[roleRef].loadChecked(this.activeRole);
          }
        })
      },
      handleOk() {
        let param = {
          roleIds : this.activeRole.join(","),
          portalId : this.portalSiteId,
          portalType:this.portalType,
          dbRoleIds: this.dbRoleIds.join(",")
        }
        postAction(this.url.addPortalAuth,param).then((res)=>{
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok');
          } else {
            this.$message.warning(res.message)
          }
        })
      },
    }
  }
</script>

<style scoped>

</style>